/*
 * ICS 314 Avionics Assignment
 * @author ICS 314 Team Haines {Kevon Adonis, Alex Char, David Teroaka, Samuel Roberts}
 * @copyright 2014
 */

package avionics;

import java.util.Random;

/**
Last revision: 2014-04-21 by David

This class simulates a VOR radio station which generates radio pulses for incoming
aircraft to listen to.

The radio station contains 4 main pieces of information:
- True name of the station
- Morse code abbreviation of the name
- Broadcasted radial (0 - 359, integer value)
- GOOD or BAD signal (boolean TRUE or FALSE)

The name, Morse code name, and the broadcasted radial cannot be changed once set.  The
signal will change each time the accessor method is called.  Every time the signal method
is called to check for GOOD or BAD, a random number generator determines what signal will
be returned.  Right now it is set to: 90% - GOOD, 10% - BAD

For internal testing, set the private variable "debug" to "true" to print to the error
output.  Otherwise when the code goes live, set "debug" to "false".

 */
public class AvionicsRadioSimulator {

    private boolean debug = false;

    //Constructor variables
    private String name;
    private String morseName;
    private int radial;
    private String signalQuality; // changed to String 4/21 - Kevon

    //Random number generator.  Not cryptographically secure, but whatever, should not be a problem.
    private Random rng = new Random();


    /**
    AvionicsRadioSimulator Constructor

    Input: True name (String), and Morse code name (2-3 letter String)
    */
    
    public AvionicsRadioSimulator(String morseName)
    {
        this.name = "DefaultName";
        if(morseName.length() != 2)
        {
            this.morseName = "ZZZ";
        }
        else
        {
            this.morseName = morseName;
        }
        this.morseName = MorseCodeConverter.convertToMorse(this.morseName);
        this.radial = rng.nextInt(360);  //generates a value from 0 to 359
        getSignalQuality(rng.nextInt(10));  //forces the constructor to set signal to GOOD or BAD

        if(debug)  {
            System.err.println("DEBUG: AvionicsRadioSimulator constructor called");
            System.err.println("DEBUG: name = " + this.name);
            System.err.println("DEBUG: morseName = " + this.morseName);
            System.err.println("DEBUG: radial = " + this.radial);
            System.err.println("DEBUG: signalQuality = " + this.signalQuality); 
        }
    }

    public AvionicsRadioSimulator(String name, String morseName)  {
        this.name = name;  //TO DO: make sure this is not blank.  if blank, set to "DefaultName"
        if(morseName.length() != 3)
        {
            this.morseName = "ZZZ";
        }
        else
        {
            this.morseName = morseName;
        }
        this.morseName = MorseCodeConverter.convertToMorse(this.morseName);
        this.radial = rng.nextInt(360);  //generates a value from 0 to 359
        getSignalQuality(rng.nextInt(10));  //forces the constructor to set signal to GOOD or BAD

        if(debug)  {
            System.err.println("DEBUG: AvionicsRadioSimulator constructor called");
            System.err.println("DEBUG: name = " + this.name);
            System.err.println("DEBUG: morseName = " + this.morseName);
            System.err.println("DEBUG: radial = " + this.radial);
            System.err.println("DEBUG: signalQuality = " + this.signalQuality);
        }


    }  //end of constructor

    /**
    Accessor methods.
    */
    public String getName()  {
        return this.name;
    }

    public String getMorseName()  {
        return this.morseName;
    }

    public int getRadial()  {
        return this.radial;
    }

    public String getSignalQuality(int random)  {
        // 0-8, true, 9 is false
        /*int random = rng.nextInt(10);*/

        if(debug)  {
            System.err.println("DEBUG: signal RNG = " + random);
        }

        if(random >= 0 && random < 9)  {
            this.signalQuality = "Good";
        }
        else if(random == 9)  {
            this.signalQuality = "Bad";
        }
        else  {  //should never happen, but leave this for now
            this.signalQuality = "Good";
            System.err.println("something went wrong with the signal generator");
        }

        return this.signalQuality;
    }


}//end of class
